import os.path
from typing import Any

from aiogram import Bot, types
from aiogram.types import FSInputFile
from aiogram_dialog import DialogManager, ShowMode
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.core.constants import ASSETS_FOLDER
from poker_bot.database.services import UserService
from poker_bot.tg_bot.services.tg_helpers import (
    answer_msg_with_autodelete,
    send_tg_message,
)
from poker_bot.tg_bot.states import AddAccountSG, CheckSubscribeSG, CreateDepositSG


async def start_deposit_dialog(call: types.CallbackQuery, _, manager: DialogManager):
    user_service: UserService = manager.middleware_data.get("user_service")

    user = await user_service.get_user_by_telegram_id(call.from_user.id)
    if user.deposits_count > 0 and user.is_subscribed:
        await manager.start(CreateDepositSG.select_payment_method)
    else:
        await manager.start(CheckSubscribeSG.display_social_media)


async def answer_first_question(
    call: types.CallbackQuery, widget: Any, manager: DialogManager
):
    bot: Bot = manager.middleware_data.get("bot")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    config: Settings = manager.middleware_data.get("config")
    user_service: UserService = manager.middleware_data.get("user_service")

    if widget.widget_id == "plus_reg_btn":
        user = await user_service.get_user_by_telegram_id(call.from_user.id)
        await send_tg_message(
            bot,
            config.tg_bot.admin_channel_id,
            locale.get("new-plus-reg-msg", username=user.username),
        )
        await call.message.answer(locale.get("ticket-sent-msg"))
        return

    msg = None
    filenames = []
    if widget.widget_id == "instruction_btn":
        msg = locale.get("instruction-msg")
        filenames = ["instruction_1.jpg", "instruction_2.jpg", "instruction_3.jpg"]
    elif widget.widget_id == "club_id_btn":
        msg = locale.get("club-id-msg")
        filenames = ["club_id.jpg", "id_example.jpg"]
    elif widget.widget_id == "already_member_btn":
        msg = locale.get("enter-id-msg")
        filenames = ["id_example.jpg"]

    if msg:
        if len(filenames) > 0:
            media = []
            for i, filename in enumerate(filenames):
                media.append(
                    types.InputMediaPhoto(
                        caption=msg if i == 0 else None,
                        media=FSInputFile(os.path.join(ASSETS_FOLDER, filename)),
                    )
                )
            await call.message.answer_media_group(media=media)
        else:
            await call.message.answer(msg)
        await call.message.delete()
        manager.show_mode = ShowMode.SEND
        await manager.start(AddAccountSG.enter_account_id)


async def contact_support(call: types.CallbackQuery, _, manager: DialogManager):
    bot: Bot = manager.middleware_data.get("bot")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    config: Settings = manager.middleware_data.get("config")
    user_service: UserService = manager.middleware_data.get("user_service")

    user = await user_service.get_user_by_telegram_id(call.from_user.id)
    await send_tg_message(
        bot,
        config.tg_bot.admin_channel_id,
        locale.get("new-ticket-msg", username=user.username),
    )
    await call.message.answer(locale.get("ticket-sent-msg"))
    return
